function update_content(url)
{
	var ajaxRequest = new Ajax.Updater('content', url, {method: 'get', asynchronous:false});
	initVarPkgList();
	return false;
} 

function update_search(url)
{
	var ajaxRequest = new Ajax.Updater('search-results', url, {method: 'get'});
	return false;
} 

function update_login(url)
{
	var ajaxRequest = new Ajax.Updater('divlogin', url, {method: 'get'});
	return false;
}

function update_welcome()
{
	var ajaxRequest = new Ajax.Updater('content', base_url+'user/homepage', {method: 'get'});
	return false;
}

function update_news(url)
{
	if ($('news') != null)
		var ajaxRequest = new Ajax.Updater('news', url, {method: 'get'});
	return false;
}

function update_newslist(url)
{
	if ($('newslist') != null)
		var ajaxRequest = new Ajax.Updater('newslist', url, {method: 'get'});
	return false;
}

//loading Handler
var myGlobalHandlers = {
	onCreate: function(){
		Element.show('loading');
	},
		onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.hide('loading');
		}
	}
};
Ajax.Responders.register(myGlobalHandlers);

//login
function log_in(){
	var data= $H({
		'login' : $F('login'),
		'password' : $F('password')		
	}).toQueryString();
	new Ajax.Updater('divlogin', base_url+'user/login',
	{
		method:'post',
		postBody:data,
		asynchronous:false
	});
	return false; //to cancel the classic submit
}

//FORMS
//user adding form
function userAddRequest(){
	var data= $H({
		'nickname' : $F('nickname'),
		'firstname' : $F('firstname'),
		'lastname' : $F('lastname'),
		'email' : $F('email'),
		'pwd1' : $F('pwd1'),
		'pwd2' : $F('pwd2')			
	}).toQueryString();
	new Ajax.Updater('content', base_url+'user/register',
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //userAddRequest

//user editing form
function userEditRequest(userId){
	var data= $H({
		'nickname' : $F('nickname'),
		'firstname' : $F('firstname'),
		'lastname' : $F('lastname'),
		'email' : $F('email'),
		'rights' : $F('rights'),
		'pwd1' : $F('pwd1'),
		'pwd2' : $F('pwd2')
	}).toQueryString();
	new Ajax.Updater('content', base_url+'user/edit_user/'+userId,
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //userEditRequest

//user search form
function userSearchRequest(){
	new Ajax.Updater('search-results', base_url+'user/userresults/nickname/'+$F('nickname'),
	{
		method:'get'
	});

	return false; //to cancel the classic submit
} //userSearchRequest

//package search form
function pkgSearchRequest(){
	
	new Ajax.Updater('search-results', base_url+'package/pkgresults/name/'+$F('name'),
	{
		method:'get'
	});

	return false; //to cancel the classic submit
} //pkgSearchRequest


//VALIDATION
function errorUpdate(type,field,value)
{
	var url = base_url+type+'/check/'+field+'/'+value;
	new Ajax.Updater(field+'-error',url,
	{
		method: 'get'
	});
}

// user adding form validation
var timerNickname;
function validateUser(field)
{
	var delay = 1500;
	if(field == 'nickname')
	{
		clearTimeout(timerNickname);
		timerNickname = setTimeout(errorUpdate,delay,'user',field,$F(field));
	}
}


//package adding form
function pkgAddRequest(){
	var category;
	if ($('rad-catlist').checked)
		category = $F('category-list');
	else
		category = $F('category-new');
	var data= $H({
		'name' : $F('name'),
		'website' : $F('website'),
		'licence' : $F('licence'),
		'category' : category,
		'short_desc' : $F('short_desc')			
	}).toQueryString();
	new Ajax.Updater('content', base_url+'package/add_pkg',
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //pkgAddRequest

//package editing form
function pkgEditRequest(pkgId){
	var category;
	if ($('rad-catlist').checked)
		category = $F('category-list');
	else
		category = $F('category-new');
	var data= $H({
		'name' : $F('name'),
		'website' : $F('website'),
		'licence' : $F('licence'),
		'category' : category,
		'short_desc' : $F('short_desc'),
		'status' : $F('status')
	}).toQueryString();
	new Ajax.Updater('content', base_url+'package/edit_pkg/'+pkgId,
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //pkgEditRequest



//branch adding form
function branchAddRequest(pkgid){
	var data= $H({
		'name' : $F('name'),
		'description' : $F('description')
	}).toQueryString();
	new Ajax.Updater('content', base_url+'branch/add_branch/'+pkgid,
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //branchAddRequest

//branch editing form
function branchEditRequest(branchId){
	var data= $H({
		'name' : $F('name'),
		'description' : $F('description'),
		'status' : $F('status')
	}).toQueryString();
	new Ajax.Updater('content', base_url+'branch/edit_branch/'+branchId,
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //branchEditRequest

//release adding form
function rlsAddRequest(branchid){
	var data= $H({
		'version' : $F('version'),
		'release_note' : $F('release_note'),
		'type' : $F('type'),
		'status' : $F('status')		
	}).toQueryString();
	new Ajax.Updater('content', base_url+'release/add_release/'+branchid,
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //branchAddRequest

//release editing form
function rlsEditRequest(rlsId){
	var data= $H({
		'version' : $F('version'),
		'release_note' : $F('release_note')
	}).toQueryString();
	new Ajax.Updater('content', base_url+'release/edit_release/'+rlsId,
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //rlsEditRequest

//USERS LIST
var active_user;
function showProfile(user_id){
	var id='details-user';
	if (!Element.visible(id) || active_user != user_id) {
		var url = base_url+'user/profile/'+user_id;
		new Ajax.Updater(id, url,
		{
			method:'get'
		});
		Element.show(id);
		active_user = user_id;
	}
	else {
		Element.hide(id);
		active_user = null;
	}
}

var pkgInfo='details-package';
var branchInfo='details-branch';
var releaseInfo='details-release';
var active_pkg = null;
var active_bch = null;
var active_rls = null;
function initVarPkgList(){
	if ($(pkgInfo) != null)
		Element.hide(pkgInfo);
	active_pkg = null;
	if ($(branchInfo) != null)
		Element.hide(branchInfo);
	active_bch = null;
	if ($(releaseInfo) != null)
		Element.hide(releaseInfo);
	active_rls = null;
}

function update_pkglist(url){
	initVarPkgList();
	update_content(url);
}

//BRANCHS LIST
function showDetailsPkg(pkg){
	var id='branchlist-'+pkg;
	if(!Element.visible(id)){
		var url = base_url+'branch/loadBranch/'+pkg;
		new Ajax.Updater(id, url,
		{
			method:'get'
		});
		Element.show(id);
		if (active_pkg != null && active_pkg != pkg)
			Element.hide('branchlist-'+active_pkg);
	}
	else{
		Element.hide(id);
	}
	
	if(!Element.visible(pkgInfo) || active_pkg != pkg){
		var url = base_url+'package/load/'+pkg;
		new Ajax.Updater(pkgInfo, url,
		{
			method:'get'
		});
		Element.show(pkgInfo);
		active_pkg = pkg;
	}
	else{
		Element.hide(pkgInfo);
		active_pkg = null;
	}
	Element.hide('details-branch');
	Element.hide('details-release');
}

//RELEASES LIST
function showDetailsBranch(branch){
	var id='releaselist-'+branch;
	if(!Element.visible(id)){
		var url = base_url+'release/loadRelease/'+branch;
		new Ajax.Updater(id, url,
		{
			method:'get'
		});
		Element.show(id);
		if (active_bch != null && active_bch != branch)
			Element.hide('releaselist-'+active_bch);
	}
	else{
		Element.hide(id);
	}
	
	if(!Element.visible(branchInfo) || active_bch != branch){
		var url = base_url+'branch/load/'+branch;
		new Ajax.Updater(branchInfo, url,
		{
			method:'get'
		});
		Element.show(branchInfo);
		active_bch = branch;
	}
	else{
		Element.hide(branchInfo);
		active_bch = null;
	}
	Element.hide('details-release');
}

//DETAILS OF EACH RELEASE
function showDetailsRls(rlsid){
	if(!Element.visible(releaseInfo) || active_rls != rlsid){
		var url = base_url+'release/load/'+rlsid;
		new Ajax.Updater(releaseInfo, url,
		{
			method:'get'
		});
		Element.show(releaseInfo);
		active_rls = rlsid;
	}
	else{
		Element.hide(releaseInfo);
		active_rls = null;
	}
}

function deletePackage(elem_id) {
	var ajaxRequest = new Ajax.Request(base_url+'package/delete/'+elem_id, 
	{
		method: 'get',
		asynchronous:false,
		onSuccess:function(){
			new Effect.Puff('package-'+elem_id);
		}
	});
}

function deleteBranch(elem_id) {
	var ajaxRequest = new Ajax.Request(base_url+'branch/delete/'+elem_id, 
	{
		method: 'get',
		asynchronous:false,
		onSuccess:function(){
			new Effect.Puff('branch-'+elem_id);
		}
	});
}

function deleteRelease(elem_id) {
	var ajaxRequest = new Ajax.Request(base_url+'release/delete/'+elem_id,
	{
		method: 'get',
		asynchronous:false,
		onSuccess:function(){
			new Effect.Puff('release-'+elem_id);
		}
	});
}

function deleteRelation(elem_id, bch1, reltype, bch2) {
	var ajaxRequest = new Ajax.Request(base_url+'branch/remove_rel/'+bch1+'/'+reltype+'/'+bch2, 
	{
		method: 'get',
		asynchronous:false,
		onSuccess:function(){
			new Effect.Puff('relation-'+elem_id);
		}
	});
}

//articles adding form
function articleAddRequest(){
	var data= $H({
		'title' : $F('title'),
		'text_content' : $F('text_content')	
	}).toQueryString();
	new Ajax.Updater('content', base_url+'article/add_article/',
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //articleAddRequest



//relations adding form
function relationsAddRequest(bch1, reltype, bch2, optdata){
	new Ajax.Request(base_url+'branch/add_relation/' + bch1 + '/' + reltype + '/' + bch2 + '/' + optdata,
	{
		method:'get',
		onSuccess:function() {
			new Ajax.Updater('dep-list', base_url+'branch/list_relations/'+bch1,
			{
				method:'get'
			});
		}
	});
	
	return false; //to cancel the classic submit
}



// load branches
function loadBranches(pkgid){
	new Ajax.Updater('dep-branch-list', base_url+ 'branch/list_branchs_dd/' + pkgid,
	{
		method:'get'
	});
	return false; 
}


// load versions
function loadVersions(bchid){
	new Ajax.Updater('dep-version-list', base_url+ 'release/ list_releases_dd/' + bchid,
	{
		method:'get'
	});
	return false; 

} //articleAddRequest

//article editing form
function articleEditRequest(artId){
	var data= $H({
		'title' : $F('title'),
		'text_content' : $F('text_content')
	}).toQueryString();
	new Ajax.Updater('content', base_url+'article/edit_article/'+artId,
	{
		method:'post',
		postBody:data
	});
	return false; //to cancel the classic submit
} //articleEditRequest

function deleteArticle(elem_id) {
	var ajaxRequest = new Ajax.Request(base_url+'article/delete/'+elem_id,
	{
		method: 'get',
		asynchronous:false,
		onSuccess:function(){
			new Effect.Puff('article-'+elem_id);
		}
	});
}
